#!/usr/bin/env bash

# Copyright Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set -u
set -o pipefail

$ kubectl create ns health-annotate

# $snippet annotate_pod.sh syntax="bash"
$ kubectl -n health-annotate apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: liveness-http
spec:
  selector:
    matchLabels:
      app: liveness-http
  template:
    metadata:
      labels:
        app: liveness-http
        version: v1
      annotations:
        sidecar.istio.io/rewriteAppHTTPProbers: "true"
    spec:
      containers:
      - name: liveness-http
        image: docker.io/istio/health:example
        ports:
        - containerPort: 8001
        livenessProbe:
          httpGet:
            path: /foo
            port: 8001
          initialDelaySeconds: 5
          periodSeconds: 5
EOF
# $endsnippet

$ kubectl -n health-annotate rollout status deployment liveness-http --timeout 30s

# $snippet re-deploy_with_annotation_check_status.sh syntax="bash" outputis="text"
$ kubectl -n health-annotate get pod
# $verify verifier="lineRegex"
NAME *READY *STATUS *RESTARTS *AGE
liveness-http-[0-9a-z]*-[0-9a-z]* *1/1 *Running *0 ?
# $endsnippet

# $snippet delete_deploy_with_annotation.sh syntax="bash"
$ kubectl -n health-annotate delete deploy/liveness-http
# $endsnippet
